//
//  NSArray+Variability.m
//  SPKit
//
//  Created by 李双鹏 on 15/10/5.
//  Copyright (c) 2015年 SP. All rights reserved.
//

#import "NSArray+Variability.h"

@implementation NSArray (Variability)
// 抵消。从本数组去掉传入数组中也包含的元素。
-(NSArray *)subtractFromArray:(NSArray *)array{
    NSPredicate *pdc = [NSPredicate predicateWithFormat:@"NOT (self in %@)",array];
    return [self filteredArrayUsingPredicate:pdc];
}
// 取交集
-(NSArray *)intersectWithArray:(NSArray *)array{
    NSPredicate *pdc = [NSPredicate predicateWithFormat:@"self in %@",array];
    return [self filteredArrayUsingPredicate:pdc];
}
// 取并集
-(NSArray *)unionWithArray:(NSArray *)array{
    NSMutableArray *newly;
    if ([self isKindOfClass:[NSMutableArray class]]) {
        newly = (NSMutableArray *)self;
    }else{
        newly = [NSMutableArray arrayWithArray:self];
    }
    for (id obj in array) {
        if (![newly containsObject:obj]) {
            [newly addObject:obj];
        }
    }
    return newly;
}
@end
